import TableData from "@/components/public/el-table-pagination"; //主页面列表

import TableLyout from '@/components/commonPublic/tableLyout.vue'
import tree from "@/components/public/el-trees"; //左侧机构树
import CustomDialog from "@/components/stationProductionPage/HntManage/proportionMix/index.vue";

import {
    yjQmpGyYzlglPageLists, //列表页接口
    getBaseDataitemDetailByItemCode, //数据
    getUserCompanyTreeList, //树状图
} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置

export default {
    name: "pouringOrder",
    data() {
        return {
            name: "pouringOrder",
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            // checked: true, //查看下级按钮
            showName: "未选择公司",
            checkData: [], //树状图默认选中id


            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            // 列表
            getList: {
                keyType: "", // 上方查询条件设计强度
                userId: "", // 查询待办或已办时传 列表页查询不传
                modelType: "2", // 0-待办 1-已办 传2 表示列表页查询 
                fCompanyid: "", // 施工标段id
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                strengthgrade: "", // 强度等级
                machineId: "", // 拌和机编号
                stationId: "", //拌合站
                keyWord: "", // 工程部位/工程名称 模糊条件
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                warningLevel: "", // 预警等级
                fCloseoperation: "", // 闭合状态
                fStatus: ""
            },
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,

        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        CustomDialog
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.getList.fCompanyid = this.userInfo.companyId
        this.getBaseDataitemDetailByItemCode('Qdlevel', 2) //设计强度等级(Qdlevel)
        this.getDate()
            // this.getTree();
        this.btnRole(this.name)
    },
    methods: {
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        // 左侧树结构
        getTree() {
            // this.treeText.companyId = this.userInfo.companyId;
            // this.treeText.userId = this.userInfo.userId;
            this.treeText = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId,
                // text: "1", // 0其他  1 砼拌合站 2水稳 3沥青
            };
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("yjSmart-treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }

                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名
            this.getList.fCompanyid = data.fCompanyid;
            this.getList.page = 1;
            this.getList.pageSize = 20;
            this.yj_searchForm(this.getList);
        },
        handleRow() {},
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 新增/编辑
        yj_edit(num) {
            if (this.userInfo.companyNature == '施工单位') {
                this.statusNum = num
                this.dialogTitle = "委托信息"
                if (num == 1) { // 编辑
                    if (this.dataFid != null) {
                        this.editText();
                    } else {
                        this.$message.warning("请先选择需要编辑的数据");
                    }
                } else if (num == 2) {

                    if (this.dataFid != null) {

                        this.yj_submit(0)
                    } else {
                        this.$message.warning("请先选择需要提交的数据");

                    }
                } else if (num == 7) { //查看

                    if (this.dataFid != null) {
                        this.statusNum = 7
                        this.isShow = true;
                        this.editText();


                    } else {
                        this.$message.warning("请先选择需要查看的数据");

                    }
                }
            } else {
                this.$message.warning("该用户无法新增");
            }
        },
        editText() {
            this.isShow = true;
        },
        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            if (label == "浇筑方式") {
                this.statusNum = 7
                this.isShow = true;
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
        // 数据字典
        getBaseDataitemDetailByItemCode(status) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {

                var data = res.data.baseDataitemdetailList
                var arr = [{
                    value: "",
                    label: "设计强度"
                }]
                data.map(item => {
                    arr.push({
                        value: item.fItemvalue,
                        label: item.fItemvalue
                    })
                })
                this.searchObj[0].options = arr

            })
        },
        // 获取列表数据
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                fStatus: data.fStatus,
                // keyType: data.keyType, //上方查询条件材料种类
                // taskNumber: data.taskNumber, //上方查询条件进场编号
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                // userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                modelType: 2, //0-待办 1-已办 传2 表示列表页查询
            }
            this.loading = true;
            var that = this;
            yjQmpGyYzlglPageLists(obj)
                .then((res) => {
                    console.log(res);
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0]; // 开始时间
                this.getList.endDates = e[1]; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]]
            this.yj_searchForm(this.getList);

        },
        getDate() {

            if (JSON.parse(sessionStorage.getItem("yjSmart-treeCheck"))) {
                var id = JSON.parse(sessionStorage.getItem("yjSmart-treeCheck"));
                this.checkData = [id.fCompanyid];
            }
            this.getTree();
            if (JSON.parse(sessionStorage.getItem("yjSmart-getList"))) {
                var data = JSON.parse(sessionStorage.getItem("yjSmart-getList"));
                this.getList = {
                    fCompanyid: data.fCompanyid, //左侧选中组织机构id
                    keyType: data.keyType, //上方查询条件材料种类

                    page: 1, //当前页码
                    pageSize: 20, //每页显示条数
                    userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                    modelType: 2, //0-待办 1-已办 传2 表示列表页查询



                };
            } else {
                // this.getList.startDates = this.common.getDay(-6); // 开始时间
                // this.getList.endDates = this.common.getDay(0); // 截至时间
                // this.getList.timeDates = [this.getList.startDates, this.getList.endDates]
            }
            // this.yj_searchForm(this.getList);
        },

    },
};